
     
package com.huawei.demo;

import com.huaweicloud.sdk.cloudbuild.v3.CloudBuildClient;
import com.huaweicloud.sdk.cloudbuild.v3.model.RunJobRequest;
import com.huaweicloud.sdk.cloudbuild.v3.model.RunJobRequestBody;
import com.huaweicloud.sdk.cloudbuild.v3.model.RunJobResponse;
import com.huaweicloud.sdk.cloudbuild.v3.region.CloudBuildRegion;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CloudBuildRunJobDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(CloudBuildRunJobDemo.class.getName());

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);

        // 创建CloudBuildClient实例
        CloudBuildClient client = CloudBuildClient.newBuilder()
                                                .withCredential(auth)
                                                .withRegion(CloudBuildRegion.CN_NORTH_1)
                                                .withHttpConfig(new HttpConfig().withIgnoreSSLVerification(
                    true).withTimeout(60000)).build();

        // 创建执行构建任务请求
        RunJobRequest request = new RunJobRequest();
        RunJobRequestBody body = new RunJobRequestBody();
        body.setJobId("<YOUR JOB ID>");
        request.withBody(body);

        try {
            RunJobResponse response = client.runJob(request);
            LOGGER.info(response.toString());
        } catch (ConnectionException e) {
            LOGGER.error(e.toString());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.toString());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.toString());
            LOGGER.error(String.valueOf(e.getHttpStatusCode()));
            LOGGER.error(e.getErrorCode());
            LOGGER.error(e.getErrorMsg());
        }
    }
}
